"""
绘制轮廓的拟合多边形
"""
import cv2

img = cv2.imread('../data/cloud.png')
cv2.imshow('img',img)

img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
t,binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)

# 提取轮廓坐标
cnts,hie = cv2.findContours(binary,
                                  cv2.RETR_EXTERNAL,
                                  cv2.CHAIN_APPROX_NONE)

# 根据轮廓的坐标，生成拟合多边形的点集

# 精度1
adp1 = img.copy()
# cv2.arcLength :计算周长
eps = 0.005 * cv2.arcLength(cnts[0],True)

approx = cv2.approxPolyDP(cnts[0],
                          eps, # 精度
                          True) # 是否闭合

# print(approx)

cv2.drawContours(adp1,[approx],0,(0,0,255),2)
cv2.imshow('res_0.005',adp1)

# 精度2
adp2 = img.copy()

# cv2.arcLength :计算周长
eps = 0.01 * cv2.arcLength(cnts[0],True)

approx = cv2.approxPolyDP(cnts[0],
                          eps, # 精度,eps越大精度越低，eps越小精度越高
                          True) # 是否闭合

# print(approx)

cv2.drawContours(adp2,[approx],0,(0,0,255),2)
cv2.imshow('res_0.01',adp2)

cv2.waitKey()
cv2.destroyAllWindows()